package com.example.demo.model.component;

import api.*;
import cn.hutool.core.date.DateTime;
import com.example.demo.gpss.GPDBSession;
import com.example.demo.gpss.GPSSConnector;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author yuanz
 */
@Component
public class GPSSComponent {


    public String testGPSSInsert() {
        long start = System.currentTimeMillis();
        GPSSConnector gpss = new GPSSConnector();
        gpss.establishGpssChannel();
        // 获取bStub
        GpssGrpc.GpssBlockingStub gpssStub = gpss.getGpssStub();
        // 获取session
        GPDBSession gpDBSession = new GPDBSession(gpssStub);
        Session session = gpDBSession.getGpssConnection();
        String schemaName = "public";
        String tableName = "student";

        // 错误个数
        int errLimit = 20;

        // 错误百分比
        int errPct = 20;

        // 指定insert的表结构，需要和表字段名一一对应
        InsertOption insertOpt = InsertOption.newBuilder()
                .setErrorLimitCount(errLimit)
                .setErrorLimitPercentage(errPct)
                .setTruncateTable(false)
                .addInsertColumns("id")
                .addInsertColumns("name")
                .addInsertColumns("age")
                .addInsertColumns("create_time")
//                .addInsertColumns("update_time")
                .build();

        // create an open request builder
        OpenRequest openReq = OpenRequest.newBuilder()
                .setSession(session)
                .setSchemaName(schemaName)
                .setTableName(tableName)
//                .setPreSQL("")
//                .setPostSQL("")
//                .setStagingSchema("")
                .setEncoding("utf-8")
                .setTimeout(5)
                .setInsertOption(insertOpt)
                .build();

        // use the blocking stub to call the open service: it returns nothing
        gpssStub.open(openReq);

        // 组装写入批次
        List<RowData> rows = new ArrayList<RowData>();
        for (int i = 0; i < 10000; i++) {
            //create a row builder
            Row.Builder builder = Row.newBuilder();

            // create builders for each column. in order, and set values
            DBValue.Builder colBuilder1 = DBValue.newBuilder();
            colBuilder1.setStringValue(UUID.randomUUID().toString());
            builder.addColumns(colBuilder1);

            DBValue.Builder colBuilder2 = DBValue.newBuilder();
            colBuilder2.setStringValue("this is name");
            builder.addColumns(colBuilder2);

            DBValue.Builder colBuilder3 = DBValue.newBuilder();
            colBuilder3.setInt32Value(18);
            builder.addColumns(colBuilder3);

            DBValue.Builder colBuilder4 = DBValue.newBuilder();
            colBuilder4.setStringValue(DateTime.now().toStringDefaultTimeZone());
            builder.addColumns(colBuilder4);
            // builder the row
            RowData.Builder rowBuilder = RowData.newBuilder().setData(builder.build().toByteString());
            // add the row
            rows.add(rowBuilder.build());
        }

        // create a write request builder
        WriteRequest writeReq = WriteRequest.newBuilder()
                .setSession(session)
                .addAllRows(rows)
                .build();

        //use the blocking stub to call the write service it returns nothing
        gpssStub.write(writeReq);

        // create a close request builder
        TransferStats tStats = null;
        CloseRequest closeReq = CloseRequest.newBuilder()
                .setSession(session)
                .build();

        // use the blocking stub to call the  Close service
        tStats = gpssStub.close(closeReq);
        long end = System.currentTimeMillis();
        long use = end - start;
        System.out.println("ClosrRequest tStats: " + tStats.toString());
        return "GP数据库入库:" + "-" + 10000 + "条数据总耗时：" + use + "毫秒。";
    }
}
